package com.example.product_practices.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.product_practices.domain.User;
import com.example.product_practices.vo.SocialUser;
import com.example.product_practices.vo.UserLoginVo;
import com.example.product_practices.vo.UserRegister;
import com.example.product_practices.exception.BizCodeEnume;
import com.example.product_practices.exception.PhoneException;
import com.example.product_practices.exception.UsernameException;
import com.example.product_practices.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.common.entity.PageEntity;

import java.sql.Wrapper;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, User user) {
        return AjaxResult.success(userService.page(pageEntity.toPage(), Wrappers.lambdaQuery(user)));
    }

    @GetMapping("/list")
    public AjaxResult list(User user) {
        return AjaxResult.success(userService.list(Wrappers.lambdaQuery(user)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        return AjaxResult.success(userService.save(user));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody User user) {
        return AjaxResult.success(userService.updateById(user));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(userService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(userService.getById(id));
    }

    @PostMapping("addRepliersOrUsers")
    public AjaxResult addRepliers(@RequestBody User[] users){
        for (User user : users) {
            userService.save(user);
        }
        return AjaxResult.success(users.length);
    }

    @PostMapping("addRepliersToGroup")
    public AjaxResult addRepliersToGroup( Integer[] replierIds,Integer groupId){
        for (Integer id : replierIds) {
            UpdateWrapper<User> wrappers = new UpdateWrapper<>();
            wrappers.eq("id",id);
            wrappers.set("group_id",0);
            userService.update(wrappers);
        }
        return AjaxResult.success(replierIds.length);
    }



    @DeleteMapping("/logicDeleteUser/{ids}")
    public AjaxResult logicDeleteUser(@PathVariable Integer[] ids){

        for (Integer id : ids) {
            UpdateWrapper<User> wrappers = new UpdateWrapper<>();
            wrappers.eq("id",id);
            wrappers.set("used",0);
            userService.update(wrappers);
        }
        return AjaxResult.success(ids.length);
    }


    /**
     * 租户模糊查询
     * @param pageEntity
     * @param partName
     * @return
     */
    @GetMapping("/renterPageLike")
    public AjaxResult getRenterPage(PageEntity pageEntity,String partName){
        QueryWrapper<User> wrappers = new QueryWrapper<>();
        wrappers.eq("auth",2);
        wrappers.like("name",partName);
        //要那种在使用的
        wrappers.eq("used",1);

        Page<User> page = userService.page(pageEntity.toPage(), wrappers);

        return AjaxResult.success(page);
    }

    /**
     * 用户模糊查询
     * @param pageEntity
     * @param partName
     * @param superId
     * @return
     */
    @GetMapping("/replierPageLike")
    public AjaxResult getReplierPage(PageEntity pageEntity,String partName,Integer superId){
        QueryWrapper<User> wrappers = new QueryWrapper<>();
        wrappers.eq("auth",4);
        wrappers.like("name",partName);
        wrappers.eq("super_id",superId);
        //要那种在使用的
        wrappers.eq("used",1);
        Page<User> page = userService.page(pageEntity.toPage(), wrappers);

        return AjaxResult.success(page);
    }

    @GetMapping("/userPageLike")
    public AjaxResult getUserPage(PageEntity pageEntity,String partName,Integer superId){
        QueryWrapper<User> wrappers = new QueryWrapper<>();
        wrappers.eq("auth",3);
        wrappers.like("name",partName);
        wrappers.eq("super_id",superId);
        //要那种在使用的
        wrappers.eq("used",1);
        Page<User> page = userService.page(pageEntity.toPage(), wrappers);

        return AjaxResult.success(page);
    }


    @GetMapping("/allRenter")
    public AjaxResult getAllRenter(){
        QueryWrapper<User> wrappers = new QueryWrapper<>();
        wrappers.eq("auth",2);
        wrappers.eq("used",1);
        List<User> list = userService.list(wrappers);
        return AjaxResult.success(list);
    }

    @GetMapping(value = "/getIdByName/{name}")
    public AjaxResult getIdByName(@PathVariable("name") String name) {
        return AjaxResult.success(userService.getId(name));
    }


}
